<?php
$pageId = 'userCenter';
$user = $data['user'];
//dump($data);

?>
<!DOCTYPE html>
<html lang="zhcn">
<head>
    <include file="Common/meta" />
    <title>个人中心 - {$_ENV['site'][0]['title']}</title>
    <include file="Common/link" />
    <style>
        body {
            padding-top: 0;
        }
    </style>
</head>
<body class="bg-gray">
<div class="layout-body">
    <div class="container">
        <div class="user-info-box">
            <header class="layout-header header-inline">
                <h1 class="title"></h1>
                <ul class="tool-list">
                    <li class="tool-item"><a href="/user/updatePage"><i class="icon icon-edit"></i></a></li>
                </ul>
            </header>
            <img class="user-avatar" src="{$user['photo']}">
            <div class="username">{$user['name']}
                <if condition="$user['sex'] eq 0">
                    <i class="icon icon-male"></i>
                    <else />
                    <i class="icon icon-female"></i>
                </if>
            </div>
        </div>
        <ul class="list">
            <li class="list-item"><a class="item-link" href="/user/honor">
                个人荣誉
                <i class="icon icon-right"></i>
            </a> </li>
            <li class="list-item"><a class="item-link" href="/stdproduct/listPage">
                作品集
                <i class="icon icon-right"></i>
            </a> </li>
            <li class="list-item"><a class="item-link" href="/user/msgList">
                我的消息
                <i class="icon icon-right"></i>
            </a> </li>
            <li class="list-item"><a class="item-link" href="/competition/myJoinList">
                竞赛
                <i class="icon icon-right"></i>
            </a> </li>
            <li class="list-item"><a class="item-link" href="/like/likeMe">
                LIKE
                <i class="icon icon-right"></i>
            </a> </li>
        </ul>
        <div>
            <button id="tc-logout" class="btn btn-danger btn-loginout">退出登陆</button>
        </div>
    </div>
</div>
<include file="Common/nav" />
<!-- 添加荣誉弹出框 -->
<div id="honor-wrap" class="like-wrap">
    <section class="box box-like">
        <div class="box-body">
            <div class="tc-logo-box">
                <img class="logo" src="/public/asset/img/logo_blue.png">
                <div class="text">添加荣誉</div>
            </div>
            <input id="honor-dialog-input" class="input" placeholder="如：全国创新大赛一等奖" autocomplete="off">
            <input id="honor-dialog-btn" class="login-btn" type="button" value="确定">
        </div><!-- ./box-body -->
    </section>
</div><!-- ./like-wrap -->
<!-- 修改密码弹出框 -->
<div id="pwd-wrap" class="like-wrap">
    <div>

    </div>
    <section class="box box-like">
        <div class="box-body">
            <div class="tc-logo-box">
                <img class="logo" src="/public/asset/img/logo_blue.png">
                <div class="text">修改密码</div>
            </div>
            <input id="pwd-dialog-old-pwd" class="input" type="password" placeholder="旧密码">
            <input id="pwd-dialog-new-pwd" class="input" type="password" placeholder="新密码">
            <input id="pwd-dialog-repeat-pwd" class="input" type="password" placeholder="重复密码">
            <input id="pwd-dialog-modify" class="login-btn" type="button" value="修改">
        </div><!-- ./box-body -->
    </section>
</div><!-- ./like-wrap -->
<!-- 修改邮箱弹出框 -->
<div id="email-wrap" class="like-wrap">
    <section class="box box-like">
        <div class="box-body">
            <div class="tc-logo-box">
                <img class="logo" src="/public/asset/img/logo_blue.png">
                <div class="text">修改邮箱</div>
            </div>
            <input id="email-dialog-email" class="input" placeholder="新邮箱">
            <input id="email-dialog-modify" class="login-btn" type="button" value="修改">
        </div><!-- ./box-body -->
    </section>
</div><!-- ./like-wrap -->
<!-- 学校选择框 -->
<div id="school-wrap" class="like-wrap lg">
    <section class="box box-like">
        <div class="box-body">
            <div class="tc-logo-box">
                <img class="logo" src="/public/asset/img/logo_blue.png">
                <div class="text">选择学校</div>
            </div>
            <input id="school-selector-search" class="input" placeholder="输入学校名称进行搜索">
            <ul id="school-selector-list" class="school-selector-list">
                <li><a href="#">广东工业大学</a> </li>
                <li><a href="#">广东工业大学</a> </li>
                <li><a href="#">广东工业大学</a> </li>
                <li><a href="#">广东工业大学</a> </li>
                <li><a href="#">广东工业大学</a> </li>
                <li><a href="#">广东工业大学</a> </li>
            </ul>
            <div id="school-selector-empty" class="school-selector-empty tc-hide">搜索不到学校</div>
        </div><!-- ./box-body -->
    </section>
</div><!-- ./like-wrap -->


<script>
    var page = {
        isOpen: {$user['is_open']},
        honor: '{$user['honor']}'
    }
</script>
<include file="Common/script" />
<script>
    // 修改邮箱
    $('#email-dialog-modify').on('click', function (e) {
        e.preventDefault();

        var email = $('#email-dialog-email').val();
        if (!email) {
            eui.msg('请输入新邮箱');
            return;
        }

        $.ajax({
            url: '/user/emailChange',
            data: {
                email: email,
            },
            type: 'post',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.alert(obj.msg);
                } else {
                    eui.msg('修改失败，' + obj.msg);
                }
            },
            error: function () {
                eui.alert('系统出错');
            }
        });
    });

    // 修改密码
    $('#pwd-dialog-modify').on('click', function (e) {
        e.preventDefault();

        var oldPwd = $('#pwd-dialog-old-pwd').val();
        if (!oldPwd) {
            eui.msg('请输入旧密码');
            return;
        }

        var newPwd = $('#pwd-dialog-new-pwd').val();
        if (!newPwd) {
            eui.msg('请输入新密码');
            return;
        }

        var repeatPwd = $('#pwd-dialog-repeat-pwd').val();
        if (!repeatPwd) {
            eui.msg('请输入重复密码');
            return;
        }

        if (newPwd != repeatPwd) {
            eui.msg('两次密码输入不一致');
            return;
        }

        $.ajax({
            url: '/user/changePswd',
            data: {
                oldPswd: oldPwd,
                password: newPwd
            },
            type: 'post',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('修改成功');
                    $.ajax({
                        url: '/user/logout',
                        //type: 'POST',
                        dataType: 'json',
                        success: function (obj) {
                            if (obj.code === 0) {
                                window.location.reload(true);
                            } else {
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            eui.alert('系统出错');
                        }
                    });
                } else {
                    eui.msg('修改失败，' + obj.msg);
                }
            },
            error: function () {
                eui.alert('系统出错');
            }
        });

    });

    $('#modify-pwd').on('click', function (e) {
        e.preventDefault();

        $('#pwd-wrap').dialog();
    });

    $('#modify-email').on('click', function (e) {
        e.preventDefault();

        $('#email-wrap').dialog();
    });

    $('#edit-honor').on('click', function (e) {
        e.preventDefault();
        $('#honor-info-box').hide();
        $('#honor-edit-box').show();
        $(this).hide();
        $('#honor-save').show();
    });

    // 添加荣誉
    $('#add-honor').on('click', function (e) {
        e.preventDefault();

        $('#honor-wrap').dialog();
    });

    // 添加荣誉完成
    $('#honor-dialog-btn').on('click', function (e) {
        e.preventDefault();

        var honor = $('#honor-dialog-input').val();
        if (!honor) {
            eui.msg('请输入内容');
            return;
        }

        if (page.honor) {
            page.honor += ',' + honor;
        } else {
            page.honor = honor;
        }
        $.ajax({
            url: '/user/update',
            data: {
                honor: page.honor,
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('添加成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 800);

                    $('#honor-wrap').dialog();

                } else {
                    eui.msg('添加失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })


    });

    // 保存荣誉
    $('#honor-save').on('click', function (e) {
        e.preventDefault();
        var honorArr = [];
        $('#honor-list').find('input').each(function () {
            var honor = $(this).val();
            if (honor) {
                honorArr.push(honor);
            }
        });

        $.ajax({
            url: '/user/update',
            data: {
                honor: honorArr.join(','),
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('修改成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 1000);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });

    // 修改个人信息
    $('#edit1').on('click', function (e) {
        e.preventDefault();
        $(this).hide();
        $('#info1').hide();
        $('#edit-box-1').show();
        $('#save1').show();
    });
    $('#edit2').on('click', function (e) {
        e.preventDefault();
        $(this).hide();
        $('#info2').hide();
        $('#edit-box-2').show();
        $('#save2').show();
    });

    $('#save1').on('click', function (e) {
        e.preventDefault();

        var name = $('[name="name"]').val();
        if (!name) {
            eui.msg('请输入姓名');
            return;
        }
        var area = $('[name="area"]').val();
        var phone = $('[name="phone"]').val();
       /* if (!phone) {
            eui.msg('请输入电话');
            return;
        }*/

        var sex = $('[name="sex"]').val();

        var formData = new FormData();

        $.ajax({
            url: '/user/update',
            data: {
                name: name,
                sex: sex,
                area: area,
                phone: phone
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('修改成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 1000);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });

    $('#save2').on('click', function (e) {
        e.preventDefault();

        var education = $('[name="education"]').val();
        var professional = $('[name="professional"]').val();
        var school = $('[name="school"]').val();
        var desc = $('[name="desc"]').val();

        var formData = new FormData();

        $.ajax({
            url: '/user/update',
            data: {
                education: education,
                professional: professional,
                school: school,
                desc: desc,
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('修改成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 1000);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });

    $('#school').on('click', function () {
        $('#school-wrap').dialog();
    });

    // 设置是否对外显示重要信息
    $('#modify-open').on('click', function (e) {
        e.preventDefault();

        $.ajax({
            url: '/user/hideInfoToggle',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    //location.reload(true);
                    if (page.isOpen === 1) {
                        page.isOpen = 0;
                        $('#open-state').text('否');
                    } else {
                        page.isOpen = 1;
                        $('#open-state').text('是');
                    }
                } else {
                    eui.msg('操作失败');
                }
            },
            error: function () {
                eui.msg('系统出错');
            }
        });

    });

    // 上传头像
    $('#upload-avatar').on('click', function (e) {
        e.preventDefault();
        $('#avatar-input').trigger('click');
    });

    $('#avatar-input').on('change', function () {
       if ($(this).val()) {
           var formData = new FormData();
           var fileName = $(this).val();
           var file = $('#avatar-input')[0].files[0];
           var dot = fileName.lastIndexOf('.');
           var type = fileName.toLowerCase().substring(dot + 1);
           if (type !== 'jpg' && type !== 'png' ) {
               eui.msg('图片格式不正确');
               return;
           }
            if (file.size > 500 * 1024) {
                eui.msg('图片不能大于500K');
                return;
            }

           formData.append('file', file);

           $.ajax({
               url: '/user/update',
               data: formData,
               type: 'POST',
               dataType: 'json',
               processData: false,
               contentType: false,
               success: function (obj) {
                   if (obj.code === 0) {
                       eui.msg('修改成功');
                       setTimeout(function () {
                           location.reload(true);
                       }, 1000);
                   } else {
                       eui.msg('修改失败');
                   }

               },
               error: function () {
                   eui.alert('系统出错');
               }
           })
       }
    });

    showSchool();
    function showSchool(input) {
        $.ajax({
            url: '/common/schoolSearch',
            data: {
                key: input,
            },
            dataType: 'json',
            success: function (obj) {
                var list = obj.list;
                if (!list.length) {
                    $('#school-selector-list').hide();
                    $('#school-selector-empty').show();
                    return;
                }
                $('#school-selector-empty').hide();
                var $list = $('#school-selector-list');
                $list.show();
                $list.empty();
                var html = '';

                for (var i = 0; i < list.length; i++) {
                    //console.log(list[i].name);
                    html += '<li><a href="#">' + list[i].name + '</a></li>'
                }
                $list.html(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                eui.alert('系统出错');
            }
        });
    }

    $('#school-selector-search').on('input', function () {
        var val = $(this).val();
        showSchool(val);
    });

    $('#school-selector-list').on('click', 'li a', function (e) {
        e.preventDefault();
        $('#school').val($(this).text());

        $('#school-wrap').dialog('hide');
    });
</script>
</body>
</html>

